package com.android.questions.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DisplayUtil;
import com.android.questions.R;
import com.android.questions.databinding.DialogSignBinding;
import com.android.questions.ui.adapter.SignAdapter;
import com.android.questions.util.Command;
import com.dq.ad.AdManager;
import com.dq.ad.UMManager;
import com.hx.currency.data.model.Sign;

import java.util.List;

/**
 * FileName: DialogSign
 * Author: admin
 * Date: 2022/1/18 11:23
 * Description:
 */
public class DialogSign extends Dialog implements View.OnClickListener {

    private DialogSignBinding signBinding;
    private OnClickLister clickLister;
    private final Activity activity;
    private List<Sign> entityList;
    private int todayPos = 0;
    private boolean todaySign = false;      //当天是否已签到  false:未签到，true：已签到

    public DialogSign(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        signBinding = DialogSignBinding.inflate(activity.getLayoutInflater());
        setContentView(signBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        initView();
        initLister();
    }

    private void initView() {
        ActivityClickUtil.getInstance().clear();
        UMManager.onEvent(UMManager.Event.Sign_open);
        SignAdapter signAdapter = new SignAdapter();
        signBinding.recyclerViewSign.setLayoutManager(Command.Method.getGridLayoutManager(activity, 3, false));
        signBinding.recyclerViewSign.setAdapter(signAdapter);
        signAdapter.setTodayPos(this.todayPos);
        signAdapter.setTodayState(this.todaySign);
        signAdapter.setList(entityList);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                signBinding.ivClose.setVisibility(View.VISIBLE);
            }
        }, 1000);

        AdManager.getInstance().showNativeAD(activity, signBinding.nativeContainer, DisplayUtil.getScreenWidthDp(activity) - 48, 0, AdManager.NATIVE_AD_TYPE_UP_IMAGE, false, new AdManager.OnNativeVideoADListener() {
            @Override
            public void onSuccess() {
                signBinding.nativeContainer.setBackgroundColor(activity.getResources().getColor(R.color.Color_FFFFFF));
            }

            @Override
            public void onClosed() {
                signBinding.nativeContainer.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
            }

            @Override
            public void onError() {

            }

            @Override
            public void onClick() {

            }

            @Override
            public void onShow(View view) {

            }
        });
    }

    private void initLister() {
        signBinding.imgSign.setOnClickListener(this);
        signBinding.ivClose.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        dismiss();
        if (v.getId() == R.id.imgSign) {
            if (!this.todaySign) {
                if (this.todayPos == 0) {
                    UMManager.onEvent(UMManager.Event.Sign_1);
                } else if (this.todayPos == 1) {
                    UMManager.onEvent(UMManager.Event.Sign_2);
                } else if (this.todayPos == 2) {
                    UMManager.onEvent(UMManager.Event.Sign_3);
                } else if (this.todayPos == 3) {
                    UMManager.onEvent(UMManager.Event.Sign_4);
                } else if (this.todayPos == 4) {
                    UMManager.onEvent(UMManager.Event.Sign_5);
                } else if (this.todayPos == 5) {
                    UMManager.onEvent(UMManager.Event.Sign_6);
                } else if (this.todayPos == 6) {
                    UMManager.onEvent(UMManager.Event.Sign_7);
                } else if (this.todayPos == 7) {
                    UMManager.onEvent(UMManager.Event.Sign_8);
                } else if (this.todayPos == 8) {
                    UMManager.onEvent(UMManager.Event.Sign_9);
                }
            }

            if (this.clickLister != null) {
                this.clickLister.onSign();
            }
        } else if (v.getId() == R.id.iv_close) {
            if (clickLister != null) {
                clickLister.onClose();
            }
        }
    }

    public DialogSign setOnClickLister(OnClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    public DialogSign setList(List<Sign> list) {
        this.entityList = list;
        return this;
    }

    public DialogSign setTodayPos(int pos) {
        this.todayPos = pos;
        return this;
    }

    public DialogSign setTodaySignState(boolean signState) {
        this.todaySign = signState;
        return this;
    }

    public interface OnClickLister {
        void onSign();

        void onClose();
    }
}